﻿@using Cm.Web.Models.Demo;
@model Entity
@{
    ViewBag.Title = "Index";
    var ajaxOpt = new AjaxOptions
    {
        Confirm = "您确定要保存该数据？",
        HttpMethod = "Post",
        //LoadingElementDuration = 1000,//加载时元素显示的时间(毫秒)
        //LoadingElementId = "ld_001",
        //OnBegin = "onBegin",
        //OnComplete = "onComplete",
        //OnFailure = "onFailure",
        OnSuccess = "onSuccess",
        UpdateTargetId = "optMsg"//如果使用的是Json数据返回数据的话，就不建议使用
    }; 
    
    
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"  type="text/javascript"></script>
<script type="text/javascript">

//    $(function () {
//        $("#my input[type='text']").change(commonValChanged);
//        $("#my input[type='radio']").change(commonValChanged);
//        $("#my select").change(commonValChanged);
//        $("#my textarea").change(commonValChanged);
//        window.onbeforeunload = function () {
//            if (isValChanged) {
//                event.returnValue = "确定离开当前页面吗？";
//            }
//        };
//    });
//    var isValChanged = false;
//    function commonValChanged() {
//        isValChanged = true;
//    }
//    function onSuccess(data) {
//        if (data.sucess) {

    function onBegin() {
        showMsg('开始请求1...');
    }
    function onComplete() {

//        } else {
    }
    function onFailure() {
        showMsg('调用失败...');
    }
    function onSuccess(data) {
        if (data.Success) {

//        }
//    }
            alert(data.Msg);

  

    function test() {

    }
    function setVal() {
        $('#Text1').val("123");
    }

    function infoSuccess() {

    }
    function infoFail() {

    }
</script>
<div id="optMsg">
</div>
@using (Ajax.BeginForm("Index", "AjaxDemo", ajaxOpt))
{
    <div id="fromMsg">
        @Html.ValidationSummary(false)
    </div>
    @Html.LabelFor(m => m.ID)  @Html.TextBoxFor(m => m.ID, new { onchange = "test()" })   @Html.ValidationMessageFor(m => m.ID) 
    <br />
    @Html.LabelFor(m => m.Name)     @Html.TextBoxFor(m => m.Name)  @Html.ValidationMessageFor(m => m.Name) 
    <br /> 
    @Html.LabelFor(m => m.Age)   @Html.TextBoxFor(m => m.Age)  @Html.ValidationMessageFor(m => m.Age) 
    <br />
    @Html.LabelFor(m => m.Address)     @Html.TextBoxFor(m => m.Address)  @Html.ValidationMessageFor(m => m.Address) 
   <br />
    @Html.LabelFor(m => m.Price)     @Html.TextBoxFor(m => m.Price, new { Value = "0.0000" })  @Html.ValidationMessageFor(m => m.Price) 
    <input id="Submit1" type="submit" value="submit"  />
   
}
 <div id="my">
    <strong>离开页面事件测试</strong>
    <select id="Select1" >
        <option>1</option>
        <option>2</option>
    </select>
    男<input id="Radio1" type="radio"  name="rb"  />
    女<input id="Radio2" type="radio"  name="rb" />
    <input id="Text1" type="text"  />
   <textarea ></textarea><input id="Button1" type="button" value="button"  onclick="setVal()"/>
</div>
 
<div>
    @Ajax.ActionLink("刷新", "Info", new AjaxOptions { UpdateTargetId = "infoCtr", OnFailure = "infoFail", OnSuccess = "infoSuccess" })
    <div id="infoCtr"></div>
</div>
 